﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using BCS.Common;
using BCS.Model;

namespace BCS.Business
{
        public class BusinessCache
        {
                private static BusinessCache instance = null;

                public static BusinessCache Instance
                {
                        get
                        {
                                if (instance == null)
                                {
                                        instance = new BusinessCache();
                                }
                                return instance;
                        }
                }

                public GraphicsCard GraphicsCard { get; private set; }

                public Camera Camera { get; private set; }

                public Graphics MainPictureBoxGraphics { get; set; }

                public Graphics SourcePictureBoxGraphics { get; set; }

                public Graphics BackPictureBoxGraphics { get; set; }

                public Graphics SubPictureBoxGraphics { get; set; }

                public List<Record> Records { get; private set; }

                public List<Team> Teams { get; private set; }

                public Team CurrentTeam { get; set; }

                public String CurrentTeamName
                {
                        get
                        {
                                if (CurrentTeam == null)
                                {
                                        return "无";
                                }
                                return CurrentTeam.Name;
                        }
                }

                public String CurrentTeamStartTime
                {
                        get
                        {
                                if (CurrentTeam == null)
                                {
                                        return "无";
                                }
                                return CurrentTeam.StartTime.ToString("yyyy年MM月dd日 HH:mm:ss");
                        }
                }

                private Int32 currentTeamCount = 0;

                public Int32 CurrentTeamCount
                {
                        get
                        {
                                return currentTeamCount;
                        }
                        set
                        {
                                currentTeamCount = value;
                        }
                }

                private int totalCount = 0;

                public Int32 TotalCount
                {
                        get
                        {
                                return totalCount;
                        }
                        set
                        {
                                totalCount = value;
                        }
                }

                public Permissions Permission { get; set; }

                public bool IsAutoCount { get; set; }

                public int CountTicks { get; set; }

                public int EmptyTicks { get; set; }

                public int FullTicks { get; set; }

                public StreamWriter DebugWriter { get; set; }

                public bool Load()
                {
                        if (!ModelCache.Load())
                        {
                                return false;
                        }

                        Records = new List<Record>();
                        foreach (var item in ModelCache.RecordModels)
                        {
                                Records.Add(new Record(item));
                        }
                        TotalCount = Records.Sum(i => i.Count);

                        Teams = new List<Team>();
                        foreach (var item in ModelCache.TeamModels)
                        {
                                Teams.Add(new Team(item));
                        }
                        CurrentTeam = (from i in Teams
                                       orderby i.StartTime
                                       select i).LastOrDefault();

                        if (CurrentTeam == null)
                        {
                                CurrentTeamCount = 0;
                        }
                        else
                        {
                                CurrentTeamCount = CurrentTeam.Count;
                        }

                        Camera = new Camera(ModelCache.CameraModel);
                        GraphicsCard = new GraphicsCard(1);
                        GraphicsCard.StartGraphicsCard();
                        GraphicsCard.SetVideoSource(0);
                        GraphicsCard.StartSnapShotAsync();

                        return true;
                }

                public void InsertTeam(Team team)
                {
                        Teams.Add(team);
                        ModelCache.TeamTable.InsertOnSubmit(team.Model);
                        Save();
                }

                public void InsertRecord(int xishu)
                {
                        RecordModel model = new RecordModel()
                        {
                                CreateTime = DateTime.Now,
                                Count = Camera.Count * xishu
                        };

                        Records.Add(new Record(model));
                        ModelCache.RecordTable.InsertOnSubmit(model);
                        Save();
                }

                public void Save()
                {
                        ModelCache.DataContext.SubmitChanges();
                }
        }
}